module.exports = function (success, error) {
    if (typeof error !== 'function') {
        error = () => {
            console.log('连接失败');
        }
    }
    const mongoose = require('mongoose')
    // 导入配置文件
    const { DBHOST, DBPORT, DBNAME } = require('../config/config')

    mongoose.connect(`mongodb://${DBHOST}:${DBPORT}/${DBNAME}`)


    mongoose.connection.once('open', () => {
        success()
    })
    // 连接错误的回调
    mongoose.connection.on('error', () => {
        error()
    })
    // 连接关闭的回调
    mongoose.connection.on('close', () => {
        console.log('连接关闭');
    })
}